package com.akaovi.shareWare.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

/**
 * @author akaovi
 * @version 1.0
 * @description 进度条
 * @date 2022/10/17 21:36
 * @since 2022/10/17 21:36
 */
public class ProcessBar extends JFrame {
    private final int totalSize;
    private JProgressBar progressBar;

    public ProcessBar(int totalSize, String title) {
        // 初始化总值
        this.totalSize = totalSize;
//            this.setLayout(null);
        JLabel jb = new JLabel();
        this.setTitle(title);//窗口标题
        this.setBounds(200, 200, 600, 100); // 设置大小
        this.setResizable(false);
        this.setLocationRelativeTo(null);  // 屏幕正中间

        // 添加唯一的进度条组件
        addProcessBar();

//        setDefaultCloseOperation(EXIT_ON_CLOSE);// 用户点击窗口关闭
        this.setVisible(true);//设置是否窗口显示

        this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    }

    public void updateProgressBar(int currentSize) {
        progressBar.setValue((int) (((double) currentSize / totalSize) * 100));
        progressBar.setStringPainted(true);
    }

    public void updateProgressBar(double percent) {
        progressBar.setValue((int) percent);
        progressBar.setStringPainted(true);
    }

    private void addProcessBar() {
        progressBar = new JProgressBar();
        progressBar.setBounds(40, 166, 496, 19);
        this.add(progressBar);
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING)
            return; //直接返回，阻止默认动作，阻止窗口关闭
        super.processWindowEvent(e); //该语句会执行窗口事件的默认动作(如：隐藏)
    }
}
